package com.gome.ocean.service.authority.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.gome.ocean.dao.authority.FunctionDAO;
import com.gome.ocean.dao.authority.MenuSourceDAO;
import com.gome.ocean.dao.authority.ModuleDAO;
import com.gome.ocean.dao.authority.RoleMenuDAO;
import com.gome.ocean.dao.model.authority.Function;
import com.gome.ocean.dao.model.authority.MenuRoleVO;
import com.gome.ocean.dao.model.authority.MenuSource;
import com.gome.ocean.dao.model.authority.Module;
import com.gome.ocean.dao.model.authority.RoleMenu;
import com.gome.ocean.service.authority.MenuRoleService;

/**
 * @author liyuefeng
 */
@Service
public class MenuRoleServiceImpl implements MenuRoleService {

    private static final Logger LOG = LoggerFactory.getLogger(MenuRoleServiceImpl.class);

    @Autowired
    private MenuSourceDAO menuSourceDAO;

    @Autowired
    private RoleMenuDAO roleMenuDAO;

    @Autowired
    private ModuleDAO moduleDAO;

    @Autowired
    private FunctionDAO functionDAO;

    @Override
    public List<MenuSource> getMenuLevelList(Integer pid, Byte level) {

        List<MenuSource> list = new ArrayList<MenuSource>();
        try {
            list = menuSourceDAO.getMenuLevelList(pid, level);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            LOG.error("异常信息：" + e.toString());
        }

        return list;
    }

    @Override
    public Integer saveMenuInfo(MenuSource menuSource) {
        Integer result = new Integer(0);
        menuSource.setAddtmie(new Date());
        menuSource.setStatus((byte) 2);
        try {
            menuSourceDAO.insertSelective(menuSource);
            result = new Integer(1);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            LOG.error("异常信息：" + e.toString());
        }

        return result;
    }

    @Override
    public List<MenuSource> getMenuInfoListByPage(Map<String, Object> param) {
        List<MenuSource> menuSources = new ArrayList<MenuSource>();
        try {
            menuSources = menuSourceDAO.getMenuInfoListByPage(param);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            LOG.error("异常信息：" + e.toString());
        }
        return menuSources;
    }

    @Override
    public int getMenuInfoListCountByPage(Map<String, Object> param) {
        try {

            return menuSourceDAO.getMenuInfoListCountByPage(param);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            LOG.error("异常信息：" + e.toString());
            return 0;
        }

    }

    @Override
    public int updateByPrimaryKeySelective(MenuSource menuSource) {

        try {
            menuSourceDAO.updateByPrimaryKeySelective(menuSource);
            return 1;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            LOG.error("异常信息：" + e.toString());
            return 0;
        }

    }

    @Override
    public int saveMenuRoleInfo(RoleMenu roleMenu) {

        try {
            roleMenuDAO.insertSelective(roleMenu);
            return 1;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            LOG.error("异常信息：" + e.toString());
            return 0;
        }
    }

    @Override
    public int getRoleMenuCountByParam(RoleMenu record) {

        return roleMenuDAO.getRoleMenuCountByParam(record);
    }

    @SuppressWarnings("finally")
    @Override
    public List<MenuRoleVO> findMenuRoleByRoleId(Map<String, Object> param) {

        List<MenuRoleVO> menuRoleVOs = new ArrayList<MenuRoleVO>();
        try {
            menuRoleVOs = menuSourceDAO.findMenuRoleByPrarentId(param);

        } catch (Exception e) {
            // TODO Auto-generated catch block
            LOG.error("异常信息：" + e.toString());
        } finally {
            return menuRoleVOs;
        }

    }

    @Override
    public int updateByRoleMenuPrimaryKeySelective(RoleMenu roleMenu) {

        try {
            roleMenuDAO.updateByPrimaryKeySelective(roleMenu);
            return 1;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            LOG.error("异常信息：" + e.toString());
            return 0;
        }

    }

    @Override
    public int getRoleMenuCountByRoleId(Integer roleId) {

        return menuSourceDAO.getRoleMenuCountByRoleId(roleId);
    }

    @Override
    public List<MenuRoleVO> findMenuRoleByParams(Map<String, Object> param, int pageNum, int pageSize) {
        List<MenuRoleVO> menuRoleVOs = new ArrayList<MenuRoleVO>();
        try {
            menuRoleVOs = menuSourceDAO.findMenuRoleByParams(param, new RowBounds(pageNum, pageSize));
        } catch (Exception e) {
            // TODO Auto-generated catch block
            LOG.error("异常信息：" + e.toString());
        }
        return menuRoleVOs;
    }

    @Override
    public List<Module> getAllModule() {

        List<Module> modules = new ArrayList<Module>();
        try {
            modules = moduleDAO.findAllModule();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            LOG.error("异常信息：" + e.toString());
        }
        return modules;
    }

    @Override
    public List<Function> getFunctionGroupByModuleId(Long moduleId) {
        List<Function> functions = new ArrayList<Function>();
        try {
            functions = functionDAO.findFunctionByModuleId(moduleId);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            LOG.error("异常信息：" + e.toString());
        }
        return functions;
    }

    @Override
    public List<MenuRoleVO> getAllMenu() {
        List<MenuRoleVO> menuRoleVOs = new ArrayList<MenuRoleVO>();
        try {
            menuRoleVOs = menuSourceDAO.getAllMenu();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            LOG.error("异常信息：" + e.toString());
        }
        return menuRoleVOs;
    }

    @Override
    public Page<MenuSource> findAllMenu(int pageNum, int pageSize) {
        Page<MenuSource> page = null;
        try {
            page = (Page<MenuSource>) menuSourceDAO.findAllMenu(new RowBounds(pageNum, pageSize));
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
        }
        return page;
    }

    @Override
    public MenuSource findMenuByName(String menuName) {
        if (StringUtils.isBlank(menuName)) {
            throw new IllegalArgumentException("菜单名字不能为空");
        }
        MenuSource menuSource = null;
        try {
            menuSource = menuSourceDAO.findMenuByMenuName(menuName);
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
        }
        return menuSource;
    }

    @Override
    public boolean insertMenuRoleList(List<RoleMenu> RoleMenu) {
        boolean flag = false;
        try {
            roleMenuDAO.insertMenuRoleList(RoleMenu);
            flag = true;
        } catch (Exception e) {
            LOG.error("异常信息" + e.toString());
        }
        return flag;
    }

    @Override
    public boolean deleteMenuRoleByRoleId(Integer roleId) {
        boolean flag = false;
        try {
            roleMenuDAO.deleteMenuRoleByRoleId(roleId);
            flag = true;
        } catch (Exception e) {
            LOG.error("异常信息" + e.toString());
        }
        return flag;
    }

    @Override
    public List<RoleMenu> getRoleMenusByRoleId(Integer roleId) {

        List<RoleMenu> roleMenus = new ArrayList<RoleMenu>();
        try {
            roleMenus = roleMenuDAO.getRoleMenusByRoleId(roleId);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            LOG.error("异常信息：" + e.toString());
        }
        return roleMenus;
    }

    @Override
    public List<Map<String, Object>> getRoleFunctionList(Map<String, Object> param) {

        List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
        try {
            result = menuSourceDAO.getRoleFunctionList(param);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            LOG.error("异常信息：" + e.toString());
        }
        return result;
    }

    @Override
    public String getMenuRoleFunctions(Map<String, Object> params) {
        try {
            return roleMenuDAO.getMenuRoleFunctions(params);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            LOG.error("异常信息：" + e.toString());
            return null;
        }
    }

    @Override
    public MenuSource selectByPrimaryKey(Integer id) {
        MenuSource menuSource = new MenuSource();
        try {
            menuSource = menuSourceDAO.selectByPrimaryKey(id);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            LOG.error("异常信息：" + e.toString());
        }
        return menuSource;
    }

    @Override
    public int deleteByPrimaryKey(Integer id) {
        try {
            menuSourceDAO.deleteByPrimaryKey(id);
            return 1;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            LOG.error("异常信息：" + e.toString());
            return 0;
        }

    }

}
